
#include "ScreenRecordCache.h"

namespace detail {

ScreenRecordCache::ScreenRecordCache() {}

void ScreenRecordCache::Clear() {
  m_rows.clear();
  m_records.clear();
}

void ScreenRecordCache::AddRecord(const RecordId& id, int index, int row,
                                  const Record& record) {
  RecordInfo ri;
  ri.id = id;
  ri.index = index;
  ri.row = row;
  ri.record = record;
  m_records[id] = ri;
  m_rows[row] = id;
}
bool ScreenRecordCache::GetRecord(const RecordId& id, Record& record) {
  RecordInfoMap::const_iterator it = m_records.find(id);
  if (it != m_records.end()) {
    record = it->second.record;
    return true;
  }
  return false;
}

bool ScreenRecordCache::IsExistById(const RecordId& id) const {
  RecordInfoMap::const_iterator it = m_records.find(id);
  return it != m_records.cend();
}
bool ScreenRecordCache::GetIdByRow(int row, RecordId& id) const {
  RecordIdMap::const_iterator it = m_rows.find(row);
  if (it != m_rows.end()) {
    id = it->second;
    return true;
  }
  id = RecordId("");
  return false;
}
bool ScreenRecordCache::GetRowById(const RecordId& id, int& row) const {
  RecordInfoMap::const_iterator it = m_records.find(id);
  if (it != m_records.end()) {
    row = it->second.row;
    return true;
  }
  return false;
}

}  // namespace detail
